package com.syh.app.basic.base;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syh.app.basic.R;
import com.syh.app.basic.utils.ComUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/8 13:39
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public abstract class BaseAdp<T> extends RecyclerView.Adapter {

    private static final int EMPTY_VIEW = 0xaa01;
    protected List<T> mData = new ArrayList<>();
    private FrameLayout mEmptyLayout;
    private RecyclerView mRecyclerView;
    private int ivIconRec;
    private String tvTipStr;

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public void setNewData(List<T> data) {
        mData.clear();
        mData.addAll(data);
        if (mData.size() > 0) {
            hasAnotherView = false;
        } else {
            setEmptyView();
        }
        notifyDataSetChanged();
    }

    public void add(@NonNull T t) {
        mData.add(t);
        hasAnotherView = false;
        notifyDataSetChanged();
    }

    public void addAll(@NonNull List<T> data) {
        mData.addAll(data);
        if (mData.size() > 0) {
            hasAnotherView = false;
        } else {
            setEmptyView();
        }
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            return new EmptyViewHolder(mEmptyLayout);
        }
        return createYourViewHolder(parent, viewType);
    }

    protected abstract RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == EMPTY_VIEW) {

        } else {
            bindYourViewHolder(holder, position);
        }
    }

    protected abstract void bindYourViewHolder(@NonNull RecyclerView.ViewHolder holder, int position);

    @Override
    public int getItemViewType(int position) {
        if (hasAnotherView) return EMPTY_VIEW;
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        if (hasAnotherView) return 1;
        return mData.size();
    }

    public void bindToRecycleView(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        getRecyclerView().setAdapter(this);
    }

    private boolean hasAnotherView = false;

    private void setEmptyView() {
        if (getRecyclerView() == null)
            throw new NullPointerException("***********请先绑定 RecyclerView************");
        hasAnotherView = true;
        View emptyView = LayoutInflater.from(getRecyclerView().getContext()).inflate(R.layout.recycleview_empty_view, getRecyclerView(), false);

        ImageView ivIcon = emptyView.findViewById(R.id.ivIcon);
        TextView tvTip = emptyView.findViewById(R.id.tvTip);

        if (ivIconRec > 0) {
            ivIcon.setImageResource(ivIconRec);
        }
        if (!TextUtils.isEmpty(tvTipStr)) {
            tvTip.setText(tvTipStr);
        }

        FrameLayout.LayoutParams layoutParamsE = new FrameLayout.LayoutParams(-1, -2);
        layoutParamsE.gravity = Gravity.CENTER;
        if (mEmptyLayout == null) {
            mEmptyLayout = new FrameLayout(emptyView.getContext());
            final RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(-1, -1);
            mEmptyLayout.setLayoutParams(layoutParams);
            mEmptyLayout.setMinimumHeight(ComUtil.Dp2Px(emptyView.getContext(), 300));
        }
        mEmptyLayout.removeAllViews();
        mEmptyLayout.addView(emptyView, layoutParamsE);
    }

    public void setIvIconRec(int ivIconRec) {
        this.ivIconRec = ivIconRec;
    }

    public void setTvTipStr(String tvTipStr) {
        this.tvTipStr = tvTipStr;
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
